package manage.model;

import m.common.model.FieldMeta;
import m.common.model.LinkTableMeta;
import m.common.model.Model;
import m.common.model.TableMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.cache.CacheUtil;
import m.system.cache.FlushCacheList;
import m.system.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@TableMeta(name="os_menu_info",description="菜单信息表")
public class MenuInfo extends Model implements FlushCacheList,IMenuName {
	private static final long serialVersionUID = 1L;

	@FieldMeta(name="name",type=FieldType.STRING,length=20,description="名称")
	private String name;
	@FieldMeta(name="url_path",type=FieldType.STRING,length=200,description="地址")
	private String urlPath;
	@FieldMeta(name="sort",type=FieldType.INT,description="排序")
	private Integer sort;
	@FieldMeta(name="description",type=FieldType.STRING,length=200,description="描述")
	private String description;
	@FieldMeta(name="ico_style",type=FieldType.STRING,length=100,description="图标样式")
	private String icoStyle;
	@LinkTableMeta(name="parent_menu_oid",table=MenuInfo.class,description="所属菜单")
	private MenuInfo parentMenu;
	@LinkTableMeta(name="module_oid",table=ModuleInfo.class,description="所属模块")
	private ModuleInfo moduleInfo;
	@FieldMeta(name="is_public",type=FieldType.STRING, description="是否公共Y/N")
	private String isPublic;
	@FieldMeta(name="is_input",type=FieldType.STRING, description="是否录入Y/N")
	private String isInput;

	public String getIsInput() {
		return isInput;
	}

	public void setIsInput(String isInput) {
		this.isInput = isInput;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIcoStyle() {
		return icoStyle;
	}
	public void setIcoStyle(String icoStyle) {
		this.icoStyle = icoStyle;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public ModuleInfo getModuleInfo() {
		return moduleInfo;
	}
	public void setModuleInfo(ModuleInfo moduleInfo) {
		this.moduleInfo = moduleInfo;
	}
	public String getUrlPath() {
		return urlPath;
	}
	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}
	public String getIsPublic() {
		return isPublic;
	}
	public void setIsPublic(String isPublic) {
		this.isPublic = isPublic;
	}
	public MenuInfo getParentMenu() {
		return parentMenu;
	}
	public void setParentMenu(MenuInfo parentMenu) {
		this.parentMenu = parentMenu;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public List<MenuInfo> getCacheList(String key) throws Exception {
		//key用逗号分开 1:adminOid 管理员 ,2:orgGroupOid 登录机构 ,3:moduleOid 所属模块 ,4:menuOid 所属菜单
		String[] arr=key.split(",");
		AdminLogin admin= CacheUtil.get(AdminLogin.class,arr[0]);
		OrgGroupView org=CacheUtil.get(OrgGroupView.class,arr[1]);
		if(arr.length>3&&!StringUtil.isSpace(arr[3])){//子菜单
			QueryCondition condition=QueryCondition.and(
				QueryCondition.eq("moduleInfo.oid", arr[2]),
				QueryCondition.eq("parentMenu.oid", arr[3]),
				ModuleInfo.getAdminCondition("menu.oid",null,admin,org)
			);
			return ModelQueryList.getModelList(MenuInfo.class,
				new String[]{"oid","name","urlPath","icoStyle","description"},
				null,
				condition,
				QueryOrder.asc("sort")
			);
		}else{//菜单
			Map<String,String> map=new HashMap<>();
			map.put("menu.oid","case when ifnull(#{menu.parentMenu.oid},'')!='' then #{menu.parentMenu.oid} else #{menu.oid} end");
			QueryCondition condition=QueryCondition.and(
				QueryCondition.eq("moduleInfo.oid", arr[2]),
				QueryCondition.isEmpty("parentMenu.oid"),
				ModuleInfo.getAdminCondition("menu.oid",map,admin,org)
			);
			return ModelQueryList.getModelList(MenuInfo.class,
				new String[]{"oid","name","urlPath","icoStyle","description"},
				null,
				condition,
				QueryOrder.asc("sort")
			);
		}
	}
}
